import storage from "store";
import { RouteLocationNormalizedLoaded } from "vue-router";
import {
  ACCESS_TOKEN,
  SET_MAINAPP_UTILS,
  SET_MAIN_ROUTE,
  SET_MOCK,
  SET_SERVER_TIME_DIFF,
  SET_TOKEN,
  SET_USER_INFO,
  SUB_APP_MOCK,
} from "../mutation-types";

type State = {
  token: string;
  mainRoute: RouteLocationNormalizedLoaded | null;
  userInfo: unknown;
  useMock: boolean;
  serverTimeDiff: number;
  mainappUtils: { [propName: string]: any };
  tabs:any
};

const state: State = {
  token: "",
  mainRoute: null,
  userInfo: null,
  useMock: false,
  serverTimeDiff: 0,
  mainappUtils: {},
  tabs:[]
};

const app = {
  state,
  mutations: {
    [SET_TOKEN]: (state: State, value: string) => {
      state.token = value;
      storage.set(ACCESS_TOKEN, value);
    },
    [SET_MAIN_ROUTE]: (state: State, value: RouteLocationNormalizedLoaded) => {
      state.mainRoute = value;
    },
    [SET_USER_INFO]: (state: State, value: unknown) => {
      state.userInfo = value;
    },
    [SET_SERVER_TIME_DIFF]: (state: State, value: number) => {
      state.serverTimeDiff = value;
    },
    [SET_MOCK]: (state: State, value: boolean) => {
      state.useMock = value;
      storage.set(SUB_APP_MOCK, value);
    },
    [SET_MAINAPP_UTILS]: (state: State, value: { [propName: string]: any }) => {
      state.mainappUtils = value;
    },
    setTabs:(state:State,value:any) => {
      state.tabs = value
    }
  },
  actions: {
    /* setLang ({ commit }, lang) {
      return new Promise((resolve, reject) => {
        commit(APP_LANGUAGE, lang)
        loadLanguageAsync(lang).then(() => {
          resolve()
        }).catch((e) => {
          reject(e)
        })
      })
    } */
  },
};

export default app;
